package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
import app.Disthanitian.thaistyle.EmerMez.LoginActivity.WebPageTask;
import app.Disthanitian.thaistyle.EmerMez.RegisterActivity.RunSendEmail;

public class AddUserLog extends Activity{
	
	static UrlEmerMez URLlink = new UrlEmerMez();
	public static final String url = URLlink.getUrladdlog();
	public static String result = "";
	// Get User_ID
	MemberUser user = new MemberUser();
	 String Useremail ;
	 String UserID;
	 String password;
	 String firstname;
	 String lastname;
	 String phoneNum;
	 int timeLocateUpdate;
	 String pinCode;
	 String msgRed;
	 String msgYellow;
	 String confirmation;
	 String type;

	// Get Value Log User
	//MemberUserLog log = new MemberUserLog();
	
	private String user_id;
	private String timeLog;
	private String dateLog;
	private String latitude;
	private String longitude;
	private String status;
	private String isRisk;
	
	public void callRun(){
		
		WebPageTask task = new WebPageTask();
		task.execute();
	}
	
	public void setVariable(MemberUserLog log){
		 UserID = log.getUser_id();		
		 Log.d("InLog",""+log.getUser_id());
		 timeLog = log.getTimeLog();
		 dateLog = log.getDateLog();
		 latitude = log.getLatitude();
		 longitude = log.getLongitude();
		 status = log.getStatus();
	     isRisk = log.getIsRisk();
	}
	
	
	public String DataLog() {

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("id", UserID));
		nameValuePairs.add(new BasicNameValuePair(" timeLog",  timeLog));
		nameValuePairs.add(new BasicNameValuePair(" dateLog",  dateLog));
		nameValuePairs.add(new BasicNameValuePair(" latitude",  latitude));
		nameValuePairs.add(new BasicNameValuePair(" longitude", longitude));
		nameValuePairs.add(new BasicNameValuePair("status",status));
		nameValuePairs.add(new BasicNameValuePair(" isRisk", isRisk));

		return postData(url, nameValuePairs);

	}

	public String postData(String url, List<NameValuePair> nameValuePairs) {

		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer("");
		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));

			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {
		} catch (IOException e) {
		}
		return null;
	}

	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
		
			result =DataLog();
			return result;
		}

		protected void onPostExecute(String result) {
			
			//Toast.makeText(AddUserLog.this,result,Toast.LENGTH_LONG).show();
			
			if (result.trim().equals("ok")) {
				
				//Toast.makeText(AddUserLog.this,"Add Log success!",Toast.LENGTH_LONG).show();

			
			} 

		}
	
            
}
}
